<?php


namespace app\admin\controller;


use app\common\bean\ListMap;
use app\common\bean\SwiperBean;
use app\common\controller\AdminController;
use app\common\enum\SwiperIsShowEnum;
use app\common\manage\SwiperManage;
use app\common\utils\ExitJsonUtil;
use think\Exception;

class Swiper extends AdminController
{
    /** 获取轮播图列表
     * @return mixed
     * @throws \Exception
     */
    public function swiperList()
    {
        $listMap = new ListMap();
        $listMap->setData($this->getParams);
        $userCourseMap = SwiperManage::getInstance()->getPaginatorList($listMap);
        $this->assign("swiperList", $userCourseMap->getList());
        $this->assign("currentPage", $userCourseMap->getCurrentPage());
        $this->assign("totalCount", $userCourseMap->getTotalCount());
        $this->assign('isShowSelect', SwiperIsShowEnum::getArrayName());
        return $this->fetch();
    }

    /** 添加广告
     * @return mixed
     * @throws Exception
     */
    public function addSwiper()
    {
        if (request()->isPost()) {
            $swiperBean = new SwiperBean();
            $swiperBean->setData($this->postParams);
            $swiperBean->setImgUrl(!is_array($swiperBean->getImgUrl()) ? "" : json_encode($swiperBean->getImgUrl()));
            $result = SwiperManage::getInstance()->add($swiperBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $this->assign('isShowSelect', SwiperIsShowEnum::getArrayName());
        return $this->fetch();
    }

    /** 修改广告图
     * @return mixed
     * @throws Exception
     */
    public function editSwiper()
    {
        $swiperBean = new SwiperBean();
        if (request()->isPost()) {
            $swiperBean->setData($this->postParams);
            $swiperBean->setImgUrl(!is_array($swiperBean->getImgUrl()) ? "" : json_encode($swiperBean->getImgUrl()));
            $result = SwiperManage::getInstance()->edit($swiperBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $swiperBean->setData($this->getParams);
        $swiperBean = SwiperManage::getInstance()->getInfo($swiperBean);
        $this->assign('swiper', $swiperBean->getData());
        $this->assign('isShowSelect', SwiperIsShowEnum::getArrayName());
        return $this->fetch();
    }

    /**
     * 删除广告
     * @throws Exception
     */
    public function delSwiper()
    {
        $swiperBean = new SwiperBean();
        $swiperBean->setData($this->getParams);
        $result = SwiperManage::getInstance()->delete($swiperBean);
        ExitJsonUtil::getInstance()->exitData($result);
    }
}